﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Threading;
using System.Net;
using System.Text.RegularExpressions;

public partial class BG_Manage_BG_NewsAdd : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.getType();
        }

    }

    private void getType()									 //显示TypeName控件的下拉列表	
    {
        VPINews.BLL.NewsType bll_type = new VPINews.BLL.NewsType();       
        this.listTypeName.DataSource = bll_type.GetAllList();
        this.listTypeName.DataTextField = "TypeName";
        this.listTypeName.DataValueField = "TypeID";
        this.listTypeName.DataBind();
        
    }

    private void addNewsNum()  //新闻类型表同类型新闻数量增加一次
    {
        VPINews.BLL.NewsType bll_type = new VPINews.BLL.NewsType();
        VPINews.Model.NewsType mod_type = bll_type.GetModel(this.listTypeName.SelectedValue);
        if (mod_type.NewsNum == null)
        {
            mod_type.NewsNum = 1;
        }
        else
        {
            mod_type.NewsNum = mod_type.NewsNum + 1;        

        } 
        bll_type.Update(mod_type);
    }

    private void addNews()
    {
        Download_IMG();                                      //获得Editor中的图片地址并下载
        
        int hline = 0;
        int hlight = 0;
        if (Headline.Checked)								 //获得是否是头条新闻
        {
            hline = 1;
        }
        else
        {
            hline = 0;
        }
        if (HighLight.Checked)							  //获得是否新闻醒目
        {
            hlight = 1;
        }
        else
        {
            hlight = 0;
        }
       
        string Title = this.txtTitle.Value;
        string Contents = this.Editor1.Text;
        int UserID = 2; //int.Parse(Session["userID"].ToString());
        string Author = this.txtAuthor.Value;
        string TypeName = this.listTypeName.SelectedItem.Text.ToString();
        DateTime PubDate = DateTime.Now;
        string Source=this.txtSummary.Value;
        string Summary=this.txtSummary.Value;
        string ImageURL = this.txtImageURL.Value;

        VPINews.BLL.News bll_news = new VPINews.BLL.News();
        VPINews.Model.News mod_news = new VPINews.Model.News();
        VPINews.BLL.NewsType bll_type = new VPINews.BLL.NewsType();
        VPINews.Model.NewsType mod_type = bll_type.GetModel(this.listTypeName.SelectedValue);

        mod_news.Title =Title;
        mod_news.Contents =Contents;
        mod_news.UserID = UserID;
        mod_news.Author = Author;
        mod_news.Headline = hline;
        mod_news.Highlight = hlight;
        mod_news.TypeName = TypeName;
        mod_news.TypeNo = mod_type.NewsNum;
        mod_news.PubDate = PubDate;
        mod_news.Source = Source;
        mod_news.Summary = Summary;
        mod_news.ImageURL = ImageURL;
        bll_news.Add(mod_news);

    }
    
    protected void Submit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            addNewsNum();
            addNews();
            smslabel.Text = "添加新闻成功";
            
        }
        else
        {
            smslabel.Text = "添加新闻错误";
        }
    }

   protected void Download_IMG()
    {
        Regex re = new Regex(@"src\s*=\s*(?:([""'])(?<src>[^""']+)\1|(?<src>[^\s>]+))", RegexOptions.IgnoreCase | RegexOptions.Singleline);
        MatchCollection mc = re.Matches(this.Editor1.XHTML.ToString());
        foreach (Match m in mc)
        {
            string url = m.Groups["src"].Value;
            string filepath = url;
            DateTime now = DateTime.Now; //获取系统时间
            
            //
            string CreatePathYear = System.Web.HttpContext.Current.Server.MapPath("../Uploads/CE_IMG/" + DateTime.Now.ToString("yyyy")).ToString();
            string CreatePathMD = System.Web.HttpContext.Current.Server.MapPath("../Uploads/CE_IMG/" + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MMdd")).ToString();

            //如果以年命名的文件夹没有，则建立以年为文件名的文件
            if (!Directory.Exists(CreatePathYear))
            {
                Directory.CreateDirectory(CreatePathYear);
            }
            //如果以月日命名的文件夹没有，则建立以为月日文件名的文件
            if (!Directory.Exists(CreatePathMD))
            {
                Directory.CreateDirectory(CreatePathMD);
            }
            //下面对Editor中的图片进行操作
            if (url.Substring(0, 7).ToLower() == "http://")
            {
                string extension = "." + url.Substring(url.LastIndexOf(".") + 1);           //扩展名
                string path = HttpContext.Current.Server.MapPath("../Uploads/CE_IMG/" + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MMdd")+"/" );     //路径
                string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + extension;      //文件名
                path = path + filename;
                filepath = "../Uploads/CE_IMG/" + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MMdd") + "/" + filename;
                WebClient myWebClient = new WebClient();
                myWebClient.DownloadFile(url, path);
                this.Editor1.Text = this.Editor1.XHTML.ToString().Replace(url, filepath);
                Thread.Sleep(800);   //延时800毫秒再次循环
            }
            //Pic.Items.Add(new ListItem(filepath, filepath)); 
        }
    }

}
